New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@project-serum/anchor

Package Overview
Dependencies
Maintainers
5
Versions
73
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@project-serum/anchor

Anchor client

  • 0.24.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
117K
decreased by-24.34%
Maintainers
5
Weekly downloads
 
Created

What is @project-serum/anchor?

@project-serum/anchor is a framework for building Solana smart contracts. It provides a set of tools and libraries to simplify the development, testing, and deployment of Solana programs. Anchor abstracts away much of the boilerplate code and offers a more developer-friendly experience.

What are @project-serum/anchor's main functionalities?

Program Development

This code demonstrates how to initialize a Solana program using Anchor. It sets up the provider, loads the IDL (Interface Definition Language) for the program, and sends a transaction to initialize the program.

const anchor = require('@project-serum/anchor');
const { SystemProgram } = anchor.web3;

const main = async () => {
  const provider = anchor.Provider.env();
  anchor.setProvider(provider);

  const idl = JSON.parse(require('fs').readFileSync('./target/idl/my_program.json', 'utf8'));
  const programId = new anchor.web3.PublicKey('YourProgramPublicKey');
  const program = new anchor.Program(idl, programId);

  const tx = await program.rpc.initialize({
    accounts: {
      myAccount: anchor.web3.Keypair.generate().publicKey,
      user: provider.wallet.publicKey,
      systemProgram: SystemProgram.programId,
    },
  });
  console.log('Transaction signature', tx);
};

main().catch(err => console.error(err));

Testing

This code demonstrates how to write tests for a Solana program using Anchor. It uses Mocha for the test framework and asserts that the account is initialized correctly.

const anchor = require('@project-serum/anchor');
const { SystemProgram } = anchor.web3;
const assert = require('assert');

describe('my_program', () => {
  const provider = anchor.Provider.env();
  anchor.setProvider(provider);

  const program = anchor.workspace.MyProgram;

  it('Initializes the account', async () => {
    const myAccount = anchor.web3.Keypair.generate();

    await program.rpc.initialize({
      accounts: {
        myAccount: myAccount.publicKey,
        user: provider.wallet.publicKey,
        systemProgram: SystemProgram.programId,
      },
      signers: [myAccount],
    });

    const account = await program.account.myAccount.fetch(myAccount.publicKey);
    assert.ok(account.user.equals(provider.wallet.publicKey));
  });
});

Client-Side Interaction

This code demonstrates how to interact with a Solana program from the client side using Anchor. It initializes an account and fetches its data.

const anchor = require('@project-serum/anchor');

const main = async () => {
  const provider = anchor.Provider.env();
  anchor.setProvider(provider);

  const idl = JSON.parse(require('fs').readFileSync('./target/idl/my_program.json', 'utf8'));
  const programId = new anchor.web3.PublicKey('YourProgramPublicKey');
  const program = new anchor.Program(idl, programId);

  const myAccount = anchor.web3.Keypair.generate();

  await program.rpc.initialize({
    accounts: {
      myAccount: myAccount.publicKey,
      user: provider.wallet.publicKey,
      systemProgram: anchor.web3.SystemProgram.programId,
    },
    signers: [myAccount],
  });

  const account = await program.account.myAccount.fetch(myAccount.publicKey);
  console.log('Account data:', account);
};

main().catch(err => console.error(err));

Other packages similar to @project-serum/anchor

FAQs

Package last updated on 13 Apr 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc